package com.xy.ocp;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/7 20:41
 * @description
 */
public class Ocp {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        Circle circle = new Circle();
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(circle);
        graphicEditor.drawShape(rectangle);
    }
}

/**
 * 问题新增会修改代码,违背了ocp原则
 */
class GraphicEditor {
    public void drawShape(Shape s) {
        if (s.m_type == 1) {
            drawCircle(s);
        } else if (s.m_type == 2) {
            drawRectangle(s);
        }
    }

    private void drawCircle(Shape s) {
        System.out.println("圆形");
    }

    private void drawRectangle(Shape s) {
        System.out.println("矩形");
    }
}

class Shape {
    int m_type;
}

class Circle extends Shape{
    Circle() {
        super.m_type = 1;
    }
}


class Rectangle extends Shape{
    Rectangle() {
        super.m_type = 2;
    }
}
